load("@fbcode//fbpkg:fbpkg.bzl", "fbpkg")
load("@fbcode_macros//build_defs:native_rules.bzl", "buck_genrule")

oncall("scm_client_infra")

# The :eden rule is a convenience to ensure that the EdenFS CLI, daemon, and SCM
# integration are built.
buck_genrule(
    name = "eden",
    out = "eden-stamp",
    cmd = "echo $(location //eden/fs/cli:edenfsctl) " +
          "$(location //eden/fs/service:edenfs) " +
          "$(location //eden/fs/service:edenfs_privhelper) " +
          "> $OUT",
    compatible_with = [
        "ovr_config//os:linux",
        "ovr_config//os:macos",
    ],
    metadata = {
        "buck.cfg_modifiers": [
            "fbcode//eden/scm/lib/backingstore:edenfs-include-cas",
        ],
    },
)

fbpkg.builder(
    name = "scm.edenfs",
    architectures = [
        "x86_64",
        "aarch64",
    ],
    buck_opts = fbpkg.buck_opts(
        mode = "opt",
    ),

    # This is doing (almost) the same as eden/fs/facebook/packaging/packman.yml
    # For the config we cannot do with fbpkg.builder (e.g. set mode for a binary) we
    # should rely on Chef or an installation script to config correctly.
    #
    # TODO:
    #  - expiry
    #  - mode: edenfs_privhelper : 04755
    #  - dirs
    #    - path: /etc/eden
    #      mode: '0755'
    #    - path: /etc/eden/config.d
    #      mode: '0755'
    path_actions = {
        # these should be put under "/usr/local/bin/"
        "bin/eden": fbpkg.symlink("bin/edenfsctl"),
        "bin/edenfsctl": fbpkg.copy("//eden/fs/cli_rs/edenfsctl:edenfsctl"),
        "bin/edenfsctl.real": fbpkg.copy("//eden/fs/cli:edenfsctl"),

        # static files
        "etc/eden/NOT_MOUNTED_README.txt": "fs/facebook/packaging/NOT_MOUNTED_README.txt",
        "etc/eden/config.d": "fs/facebook/packaging/config.d",
        "etc/eden/ignore": "fs/facebook/packaging/ignore",

        # static file for DevFeature installation instructions
        "install.toml": "facebook/dev_feature_install.toml",

        # these should be put under "/usr/local/libexec/eden/"
        "libexec/eden/eden-fb303-collector": fbpkg.copy("//eden/fs/facebook:eden-fb303-collector"),
        "libexec/eden/eden_fsck": fbpkg.copy("//eden/fs/inodes/fscatalog:eden_fsck"),
        "libexec/eden/eden_rg_perf_script": "fs/scripts/facebook/rg_perf_test",
        "libexec/eden/eden_store_util": fbpkg.copy("//eden/fs/store:eden_store_util"),
        "libexec/eden/eden_trace_stream": fbpkg.copy("//eden/fs/cli/trace:trace_stream"),
        "libexec/eden/edenfs": fbpkg.copy("//eden/fs/service:edenfs"),
        "libexec/eden/edenfs_config_manager": fbpkg.copy("//eden/fs/config/facebook:edenfs_config_manager"),
        "libexec/eden/edenfs_monitor": fbpkg.copy("//eden/fs/monitor:edenfs_monitor"),
        "libexec/eden/edenfs_privhelper": fbpkg.copy("//eden/fs/service:edenfs_privhelper_with_setuid_root"),
        "libexec/eden/edenfs_restarter": fbpkg.copy("//eden/fs/facebook:edenfs_restarter"),

        # static file for eden-locale
        "locale/en/LC_MESSAGES/libc.mo": "locale/glibc_en.mo",

        # static file for Sandcastle live-installation
        "sandcastle_install.sh": "facebook/sandcastle_install.sh",
    },
    split_debuginfo = True,
)
